"""convert allowed_content as json

Revision ID: 622464216ab7
Revises: 8cd1d967d000
Create Date: 2022-06-24 10:41:29.044185

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "1af756c610d8"
down_revision = "622464216ab7"

all_as_valid_json = """
    update content_revisions
    set properties = '{}'
    where properties = '';
"""

all_as_valid_text = """
    update content_revisions
    set properties = ''
    where properties = '{}';
"""


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    connection = op.get_bind()
    connection.execute(all_as_valid_json)
    with op.batch_alter_table("content_revisions") as batch_op:
        # INFO G.M - 2022-06-30 - ensure there is no more server_default value because such value
        # will break the type convertion
        batch_op.alter_column("properties", server_default=None)
        batch_op.alter_column("properties", type_=sa.JSON, postgresql_using="properties::json")
    # ### end Alembic commands ###


def downgrade():
    with op.batch_alter_table("content_revisions") as batch_op:
        batch_op.alter_column("properties", server_default=None)
        batch_op.alter_column("properties", type_=sa.Text, postgresql_using="properties::text")
    connection = op.get_bind()
    connection.execute(all_as_valid_text)
